from server.utils.handleFile import readJSFile, writeToJSFile

table_name = 'graph'


# 获取详细信息
def get_detail(gid):
    data = readJSFile("server/database/resource/data/" + str(gid) + "/graph.json")
    return data


# 添加图的详细信息
def add_detail(gid, back_img='', zoom='', center='', canvas_size='', show_model='', nodes='', edges=''):
    return update_detail(gid, back_img, zoom, center, canvas_size, show_model, nodes, edges)


# 更新图的详细信息
def update_detail(gid, back_img='', zoom='', center='', canvas_size='', show_model='', nodes='', edges=''):
    # 仅更新值不为空的内容
    graph_detail_data = get_detail(gid)

    if zoom is not None and len(zoom) > 0:
        graph_detail_data['zoom'] = zoom

    if back_img is not None:
        graph_detail_data['back_img'] = back_img

    if center is not None and len(center) > 0:
        graph_detail_data['center'] = center

    if canvas_size is not None and len(canvas_size) > 0:
        graph_detail_data['canvas_size'] = canvas_size

    if show_model is not None and len(show_model) > 0:
        graph_detail_data['show_model'] = show_model

    if nodes is not None:
        graph_detail_data['nodes'] = nodes

    if edges is not None:
        graph_detail_data['edges'] = edges

    # 更新
    try:
        writeToJSFile(graph_detail_data, "server/database/resource/data/" + str(gid) + "/graph.json")
        print(f'更新{table_name}信息成功，更新gid：{gid}')
        return 1
    except Exception as e:
        print(f'更新{table_name}信息失败：{e}')
        return e
